<?php
/*************************************************************************************
   Copyright notice
   
   (c) 2002-2007 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
 
   This script is part of PHPWCMS. The PHPWCMS web content management system is
   free software; you can redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later version.
  
   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license 
   from the author is found in LICENSE.txt distributed with these scripts.
  
   This script is distributed in the hope that it will be useful, but WITHOUT ANY 
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
   This copyright notice MUST APPEAR in all copies of the script!
*************************************************************************************

   Google Maps Module by breitsch (mail@casa-loca.com)

*************************************************************************************/

// ----------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {
   die("You Cannot Access This Script Directly, Have a Nice Day.");
}
// ----------------------------------------------------------------

//init
// Max size PER file in KB, not bytes for simplicity!
$max_file_size="1024";

// Max size for all files COMBINED in KB, not bytes for simplicity!
$max_combined_size="2048";

//How many file uploads do you want to allow at a time?
$file_uploads="2";

// Use random file names? true=yes (recommended), false=use original file name. Random names will help prevent overwritting of existing files!
$random_name=false;

// Please keep the array structure.
$allow_types=array("jpg","gif","png");

// Path to files folder. If this fails use $fullpath below. With trailing slash
$folder=$phpwcms['modules'][$module]['path']."icons/";

// Full url to where files are stored. With Trailing Slash
$full_url=$phpwcms['modules'][$module]['path'];//"http://www.YOUR_SITE.com/uploads/";

// Only use this variable if you wish to use full server paths. Otherwise leave this empty! With trailing slash
$fullpath="";

// Function to get the extension a file.
function get_ext($key) { 
	$key=strtolower(substr(strrchr($key, "."), 1));
	// Cause there the same right?
	$key=str_replace("jpeg","jpg",$key);
	return $key;
}

$ext_count=count($allow_types);
$types ="";
$i=0;
foreach($allow_types AS $extension) {
	
	//Gets rid of the last comma for display purpose..
	
	If($i <= $ext_count-2) {
		$types .="*.".$extension.", ";
	} Else {
		$types .="*.".$extension;
	}
	$i++;
}
unset($i,$ext_count); // why not

$error="";
$display_message="";
$uploaded = false;
$j=0;

If(isset($_POST['submit']) && $_POST['submit']==true) {

  if(empty($_POST['userfile_name'])) {
		$googlemaps['error']['userfile_name'] = 1;
		$error.="Icon Name required<br />";
	}else if (!preg_match("/^[0-9A-Za-z_]+$/", $_POST['userfile_name'])) {
		$googlemaps['error']['userfile_name'] = 1;
		$error.="Icon Name invalid<br />";
	}

	For($i=0; $i <= $file_uploads-1; $i++) {
		
  if($i==0 AND empty($_FILES['userfile']['name'][$i])) {
		$googlemaps['error']['userfile'][$i] = 1;
		$error.="Icon File required<br />";
	}
    			
		If($_FILES['userfile']['name'][$i]) {
			$j++;			
			$ext=get_ext($_FILES['userfile']['name'][$i]);
			$size=$_FILES['userfile']['size'][$i];
			$max_bytes=$max_file_size*1024;
			
			// For random names
			If($random_name){
				$file_name[$i]=time()+rand(0,100000).".".$ext;
			} Else {
				//$file_name[$i]=$_FILES['userfile']['name'][$i];
				$file_name[$i]=$_POST['userfile_name'].".".$ext;
			}
			
			if($i==1){// shadow file
   			if ($ext!="png"){
				$error.= "Invalid extension for your shadow file: ".$_FILES['userfile']['name'][$i].", only *.png are allowed for shadows.<br />Your file(s) were <b>not</b> uploaded.<br />";
        }
   			//$name_shadow = str_replace(get_ext($_FILES['userfile']['name'][0]), "shadow.png", $file_name[0]);
   			$name_shadow = $_POST['userfile_name'].".shadow.png";
   			$file_name[$i]=$name_shadow;
      }
	
			//Check if the file type uploaded is a valid file type. 
						
			If(!in_array($ext, $allow_types)) {
							
				$error.= "Invalid extension for your file: ".$_FILES['userfile']['name'][$i].", only ".$types." are allowed.<br />Your file(s) were <b>not</b> uploaded.<br />";
							
				//Check the size of each file
							
			} Elseif($size > $max_bytes) {
				
				$error.= "Your file: ".$_FILES['userfile']['name'][$i]." is to big. Max file size is ".$max_file_size."kb.<br />Your file(s) were <b>not</b> uploaded.<br />";
				
				// Check if the file already exists on the server..
			} Elseif(file_exists($folder.$file_name[$i])) {
				
				$error.= "The file: ".$file_name[$i]." exists on this server, please rename your file.<br />Your file(s) were <b>not</b> uploaded.<br />";
				
			}
						
		} // If Files
	
	} // For
  		
	if($j==1 && !$error){
   	//copy empty shadow file
   	//$name_shadow = str_replace(get_ext($_FILES['userfile']['name'][0]), "shadow.png", $_FILES['userfile']['name'][0]);
   	$name_shadow = $_POST['userfile_name'].".shadow.png";
     copy  ( $phpwcms['modules'][$module]['path']."img/original.shadow.png"  , $phpwcms['modules'][$module]['path']."icons/".$name_shadow );			
  }
	
	
	//Tally the size of all the files uploaded, check if it's over the ammount.
	If($_FILES['userfile']['size']) {			
	$total_size=array_sum($_FILES['userfile']['size']);
	  			
	$max_combined_bytes=$max_combined_size*1024;
				
	If($total_size > $max_combined_bytes) {
		$error.="The max size allowed for all your files combined is ".$max_combined_size."kb<br />";
	}
	}	
	
	// If there was an error take notes here!
	
	If($error) {
		
		$display_message=$error;
		
	} Else {
		
		// No errors so lets do some uploading!
		
		For($i=0; $i <= $file_uploads-1; $i++) {
				
			If($_FILES['userfile']['name'][$i]) {
				
				If(@move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$folder.$file_name[$i])) {
					$uploaded=true;
				} Else {
					$display_message.="Couldn't copy ".$file_name[$i]." to server, please make sure ".$folder." is chmod 777 and the path is correct.\n";
				}
			}
				
		} //For
		
	} // Else
	
} // $_POST AND !$password_form


$BE['HEADER'][] = '<!--[if IE]><![endif]-->';

$BE['HEADER'][] = '<style type="text/css">
.error_message{
		color: #FF0000;
	}
</style>'; 

?>
<?
if($uploaded==true) {?>

<table border="0" cellpadding="0" cellspacing="0" width="100%" summary="" style="background:#F3F5F8;border-top:1px solid #92A1AF;border-bottom:1px solid #92A1AF;">
  <tr><td colspan="2"><img src="img/leer.gif" alt="" width="1" height="10" /></td></tr>
	<tr>
	  <td align="right" class="chatlist">&nbsp;</td>
	  <td align="left" class="v10"><b>Your file(s) have been uploaded!</b></td>
	</tr> 
	<tr>
	  <td align="right" class="chatlist">&nbsp;</td>
	  <td class="v10">
<?
For($i=0; $i <= $file_uploads-1; $i++) {
	
	If($_FILES['userfile']['name'][$i]) {
		$file=$i+1;
		
				Echo("File #".$file.": ".$file_name[$i]."<br />\n");
	}
				
}

?>
      <br /><input name="close" type="button" class="button10" value="<?php echo $BL['be_admin_struct_close'] ?>" onclick="location.href='<?php echo map_url('controller=icons') ?>';return false;" />
    </td>
  </tr>
</table>

<?} Else {?>

<form action="<?php echo map_url('controller=icons') ?>&amp;edit=1" method="post" enctype="multipart/form-data" onsubmit="if(document.getElementById('icon').value.length >= 1 && document.getElementById('shadow').value.length == 0){return confirm('<?php echo $BLM['googlemaps_upload_confirm']; ?>')};" >
<table border="0" cellpadding="0" cellspacing="0" width="100%" summary="" style="background:#F3F5F8;border-top:1px solid #92A1AF;border-bottom:1px solid #92A1AF;">

  <tr><td colspan="2"><img src="img/leer.gif" alt="" width="1" height="10" /></td></tr>


<?If($display_message){?>

  <tr>
    <td align="right" valign="top" class="chatlist"><?php echo $BLM['googlemaps_upload_msg']; ?>:&nbsp;</td>
		<td><div align="left" class="v10 error_message"><? echo $display_message;?></div></td>
	</tr>

<?}?>

  <tr><td colspan="2"><img src="img/leer.gif" alt="" width="1" height="10" /></td></tr>
  
  <tr>
	  <td align="right" class="chatlist" style="width:100px;" ><?php echo $BLM['googlemaps_upload_icon']; ?>:&nbsp;</td>
	  <td class="v10"><input name="userfile_name" type="text" class="f11b<?php 
		
		//error class
		if(!empty($googlemaps['error']['userfile_name'])) echo ' errorInputText';
		
		?>" style="width:300px;" value="<?php if(isset($_POST['userfile_name'])) echo $_POST['userfile_name']; ?>" maxlength="200" /></td>
  </tr>
  <tr> 
		<td align="right" class="chatlist">&nbsp;</td>
		<td style="font-size:9px;"><div><?php echo $BLM['googlemaps_upload_descr']; ?></div></td>
	</tr>
  <tr><td colspan="2"><img src="img/leer.gif" alt="" width="1" height="10" /></td></tr>
  <tr>
	  <td align="right" class="chatlist"><?php echo $BLM['googlemaps_upload_iconfile']; ?>:&nbsp;</td>
	  <td class="v10"><input name="userfile[]" type="file" id="icon" class="button10<?php 
		
		//error class
		if(!empty($googlemaps['error']['userfile'][0])) echo ' errorInputText';
		
		?>" size="40" /></td>
  </tr>
  <tr> 
		<td align="right" class="chatlist">&nbsp;</td>
		<td style="font-size:9px;"><?php echo $BLM['googlemaps_upload_allow']; ?>: <?=$types?></td>
	</tr>
  <tr><td colspan="2"><img src="img/leer.gif" alt="" width="1" height="10" /></td></tr>
  <tr>
	  <td align="right" class="chatlist"><?php echo $BLM['googlemaps_upload_iconsh']; ?>:&nbsp;</td>
	  <td class="v10"><input name="userfile[]" type="file" id="shadow" class="button10" size="40" /></td>
  </tr> 
  <tr> 
		<td align="right" class="chatlist">&nbsp;</td>
		<td style="font-size:9px;"><?php echo $BLM['googlemaps_upload_allow']; ?>: *.png</td>
	</tr>
	<tr><td colspan="2"><img src="img/leer.gif" alt="" width="1" height="10" /></td></tr>
	
  <tr> 
		<td>&nbsp;</td>
		<td>
		<input type="hidden" name="submit" value="true" />
			<input type="submit" value="<?php echo $BLM['googlemaps_upload_send']; ?>" class="button10" />&nbsp;&nbsp;&nbsp;&nbsp;
			<input name="close" type="button" class="button10" value="<?php echo $BL['be_admin_struct_close'] ?>" onclick="location.href='<?php echo map_url('controller=icons') ?>';return false;" />
		</td>
	</tr>
	<tr><td colspan="2"><img src="img/leer.gif" alt="" width="1" height="10" /></td></tr>
</table>

</form>
<?} ?>




